package com.trkj.service.wdm;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.wdm.ProfessionalTitleDAO;
import com.trkj.pojo.ProfessionalTitle;
import com.trkj.service.ServiceException;

@Service
@Transactional(rollbackFor = Exception.class)
public class ProfessionalTitleService {
	@Autowired
	private ProfessionalTitleDAO professionaltitledao;
	
	/**
	 * 分页查询
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<ProfessionalTitle> queryByPage(int page,int rows){
		return professionaltitledao.queryByPage(new PageRequest(page-1, rows,Direction.DESC,"zcId"));
	}
	/**
	 * 统计所以记录条数
	 */
	public long pageByCount(){
		return professionaltitledao.count();
	}
	/**
	 * 新增
	 */
	public void insert(String zcName,String zcPerson,String zcSalary){
		try {
			ProfessionalTitle pro = new ProfessionalTitle();
			pro.setZcName(zcName);
			pro.setZcPerson(zcPerson);
			pro.setZcSalary(new BigDecimal(zcSalary));
			professionaltitledao.save(pro);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
			
		}
		
	}
	/**
	 * 删除
	 * @param zcId
	 */
	public void delete(Long zcId){
		try {
			professionaltitledao.delete(zcId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 编辑
	 */
	public void update(Long id,String zcName,String zcSalary,String zcPerson){
		try {
			ProfessionalTitle pro =professionaltitledao.findOne(id);
			pro.setZcName(zcName);
			pro.setZcPerson(zcPerson);
			pro.setZcSalary(new BigDecimal(zcSalary));
			professionaltitledao.save(pro);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 加载
	 * @param zcId
	 * @return
	 */
	public ProfessionalTitle load(Long zcId){
		return professionaltitledao.findOne(zcId);
	}
	
	/**
	 * 模糊查询
	 * @param text
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<ProfessionalTitle> selectForid_name(String text,int page,int rows){
		try {
			return professionaltitledao.query_By_idorname("%"+text+"%",new PageRequest(page-1, rows));
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 统计所以记录条数
	 */
	public long count(String text){
		return professionaltitledao.count("%"+text+"%");
	}
	
}
